//
//  AppDelegate.m
//  HDYS
//
//  Created by qianfeng on 15-4-21.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "AppDelegate.h"
#import "SideMenuView.h"
#import "MainViewController.h"
#import "SettingsViewController.h"
#import "FavoriteViewController.h"


#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

BMKMapManager* mapManager;

@interface AppDelegate () <SideMenuViewDelegate>

@property (nonatomic, strong) SideMenuView *sideMenu;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) MainViewController *mainVC;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.sessionManger = [AFHTTPSessionManager manager];
    self.sessionManger.responseSerializer = [AFHTTPResponseSerializer serializer];

    
    // 要使用百度地图，请先启动BaiduMapManager
//    mapManager = [[BMKMapManager alloc]init];
//    BOOL ret = [mapManager start:@"GUgO8qdLpbvNnIQpLSV5NrYu" generalDelegate:self];
//    
//    if (!ret) {
//        NSLog(@"manager start failed!");
//    }


    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    MainTabBarViewController *mainTabBar =[[MainTabBarViewController alloc] init];
    UIImage *img = [UIImage imageNamed:@"btn_confirm_87x36@2x"];
    UIImage *newImg = [img stretchableImageWithLeftCapWidth:10 topCapHeight:30];
    [[UITabBar appearance] setBackgroundImage:newImg];
    self.window.rootViewController = mainTabBar;
    
    //加载地图
//    LocationDemoViewController *loc=[[LocationDemoViewController alloc]initWithNibName:@"LocationDemoViewController" bundle:[NSBundle mainBundle]];
//    self.window.rootViewController=loc;
    
    
    [self.window makeKeyAndVisible];
    
    UINavigationController *mainVCNavi = [mainTabBar.viewControllers firstObject];
    MainViewController *mainVC = [[mainVCNavi viewControllers] firstObject];
    self.mainVC = mainVC;
    
    // AppDelegate成为MainViewController的代理 处理滑动手势
    mainVC.delegate = self;
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    view.alpha = 0;
    view.hidden = YES;
    view.backgroundColor = [UIColor blackColor];
    [self.window addSubview:view];
    self.backView = view;
    SideMenuView *sideMenu = [[SideMenuView alloc] initWithFrame:CGRectMake( -240, 0, 240, SCREEN_HEIGHT)];
    // 即将进入设置页面
    sideMenu.delegate = self;
    [self.window addSubview:sideMenu];
    self.sideMenu = sideMenu;

    return YES;
}

#pragma mark - MainViewControllerDelegate协议方法
-(void)mainViewControllerdidSwipeRight{
//    self.backView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.backView.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.alpha = 0.2;
        self.sideMenu.frame = CGRectMake(0, 0, 240, SCREEN_HEIGHT);
    }];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//    self.backView.frame = CGRectMake( - SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [UIView animateWithDuration:0.5 animations:^{
        self.sideMenu.frame = CGRectMake( -240, 0, 240, SCREEN_HEIGHT);
        self.backView.alpha = 0;
    }];
    self.backView.hidden = YES;
}

#pragma mark - SideMenuViewDelegate协议方法
//进入设置页面
-(void)SideMenuView:(SideMenuView *)sidemenu WillHeadToSetting:(NSInteger)setting{
    self.backView.frame = CGRectMake( - SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [UIView animateWithDuration:0.23 animations:^{
        self.sideMenu.frame = CGRectMake( -240, 0, 240, SCREEN_HEIGHT);
    }];
    
    SettingsViewController *settingVC = [[SettingsViewController alloc] init];
    settingVC.hidesBottomBarWhenPushed = YES;
    [self.mainVC.navigationController pushViewController:settingVC animated:YES];
}

-(void)SideMenuView:(SideMenuView *)sideMenu WillHeadToLike:(NSInteger)like{
    self.backView.frame = CGRectMake( - SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [UIView animateWithDuration:0.23 animations:^{
        self.sideMenu.frame = CGRectMake( -240, 0, 240, SCREEN_HEIGHT);
    }];
    
    FavoriteViewController *favVC = [[FavoriteViewController alloc] init];
    favVC.hidesBottomBarWhenPushed = YES;
    [self.mainVC.navigationController pushViewController:favVC animated:YES];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"联网成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }
    
}

- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"授权成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}

@end
